﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XTYAPI.PLC
{
    /// <summary>
    /// 使用“编码”输入bool数组，转为为数。
    /// </summary>
    public class ENCO
    {
        byte byteResult = 0 , baseByteValue = 0;
        short shorResult = 0, baseShortValue;
        int intResult = 0, baseIntValue;
        long longResult = 0, baselongValue;
        public byte ENCO_Byte(bool[] value)
        {
            byteResult = 0;
            baseByteValue = 0;
            for (int i = 0; i < value.Length; i++)
            {
                baseByteValue = (byte)(1 << i);//获取位权
                byteResult = value[i] ? (byte)(byteResult | baseByteValue) : (byte)(byteResult & ~baseByteValue);//true或  false与                        
            }
            return byteResult;
        }
        public short ENCO_Short(bool[] value)
        {
            shorResult = 0;
            baseShortValue = 0;
            for (int i = 0; i < value.Length; i++)
            {
                baseShortValue = (byte)(1 << i);//获取位权
                shorResult = value[i] ? (short)(shorResult | baseShortValue) : (short)(shorResult & ~baseShortValue);//true或  false与                        
            }
            return shorResult;
        }

        public int ENCO_Int(bool[] value)
        {
            intResult = 0;
            baseIntValue = 0;
            for (int i = 0; i < value.Length; i++)
            {
                baseIntValue = (byte)(1 << i);//获取位权
                intResult = value[i] ? (intResult | baseIntValue) : (intResult & ~baseIntValue);//true或  false与                        
            }
            return intResult;
        }
        public long ENCO_Long(bool[] value)
        {
            longResult = 0;
            baselongValue = 0;
            for (int i = 0; i < value.Length; i++)
            {
                baseIntValue = (byte)(1 << i);//获取位权
                longResult = value[i] ? (longResult | baselongValue) : (longResult & ~baselongValue);//true或  false与                        
            }
            return longResult;
        }
    }
}
